package com.qjdchina.sku.util;

import com.qjdchina.sku.dao.mapper.CategoryBrandAssociateMapper;
import com.qjdchina.sku.dao.mapper.RegionMapper;
import com.qjdchina.sku.model.CategoryBrandAssociate;
import com.qjdchina.sku.model.Region;
import com.qjdchina.sku.model.vo.CategoryBrandAssoVO;
import com.qjdchina.sku.model.vo.CategoryBrandRegionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Luoyan on 2017/5/26.
 */
@Service
public class CategoryBrandBusiUtil {
    @Autowired
    private CategoryBrandAssociateMapper categoryBrandAssociateMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 品类品牌运费调差值
     *
     * @param categoryBrandId 品类品牌关联表id
     * @return List<CategoryBrandRegionVO>
     */
    public List<CategoryBrandRegionVO> getCategoryBrandRegionVOList(Integer categoryBrandId){
        List<Region> regionList = regionMapper.selectAllRecord();
        List<Integer> regionIdList = new LinkedList<>();
        for(Region region : regionList){
            regionIdList.add(region.getId());
        }

        List<CategoryBrandAssociate> categoryBrandAssociateList = categoryBrandAssociateMapper
                .selectByCategoryBrandIdAndRegionIds(categoryBrandId, regionIdList);
        Map<Integer, CategoryBrandAssociate> categoryBrandAssociateMap = new HashMap<>();
        if(categoryBrandAssociateList!=null&&categoryBrandAssociateList.size()>0) {
            for (CategoryBrandAssociate categoryBrandAssociate : categoryBrandAssociateList) {
                categoryBrandAssociateMap.put(categoryBrandAssociate.getSkuRegionId(), categoryBrandAssociate);
            }
        }

        List<CategoryBrandRegionVO> categoryBrandRegionVOList = new LinkedList<>();
        for(Region region : regionList){
            CategoryBrandRegionVO categoryBrandRegionVO = new CategoryBrandRegionVO();
            categoryBrandRegionVO.setRegionId(region.getId());
            categoryBrandRegionVO.setRegionName(region.getRegionName());
            CategoryBrandAssociate categoryBrandAssociate = categoryBrandAssociateMap.get(region.getId());
            if(categoryBrandAssociate!=null) {
                categoryBrandRegionVO.setId(categoryBrandAssociate.getId());
                categoryBrandRegionVO.setAttrValue(categoryBrandAssociate.getAttrValue());
            }
            categoryBrandRegionVOList.add(categoryBrandRegionVO);
        }

        return categoryBrandRegionVOList;
    }

    /**
     * 获取品类品牌的定义项与具体值
     * @param skuItemNameMap 定义项映射关系
     * @param categoryBrandAssoMap 具体值映射关系
     * @return List<CategoryBrandAssoVO>
     */
    public List<CategoryBrandAssoVO> getCategoryBrandAssoVOListByItem(Map<Integer, String> skuItemNameMap, Map<Integer,
            CategoryBrandAssociate> categoryBrandAssoMap) {
        List<CategoryBrandAssoVO> categoryBrandAssoVOList = new LinkedList<>();
        for (Integer key : skuItemNameMap.keySet()) {
            CategoryBrandAssoVO categoryBrandAssoVO = new CategoryBrandAssoVO();
            categoryBrandAssoVO.setSkuItemId(key);
            categoryBrandAssoVO.setSkuItemName(skuItemNameMap.get(key));
            CategoryBrandAssociate categoryBrandAssociate = categoryBrandAssoMap.get(key);
            if (categoryBrandAssociate != null) {
                categoryBrandAssoVO.setId(categoryBrandAssociate.getId());
                categoryBrandAssoVO.setAttrValue(categoryBrandAssociate.getAttrValue());
            }
            categoryBrandAssoVOList.add(categoryBrandAssoVO);
        }
        return categoryBrandAssoVOList;
    }
}
